package service

import (
    "errors"
    "gonaio_base/service"
    "gonaio_user_service/dao"
    "gonaio_user_service/model"
)

type User struct {
}

var userInstance = new(User)

func NewUser() *User {
    return userInstance
}

func (*User) GetUserByUsername(username string) (m model.User, err error) {
    m, err = dao.NewUser().GetUserByUsername(username)
    return
}

func (that *User) Reg(username, password string) (err error) {

    m, err := that.GetUserByUsername(username)
    if m.ID > 0 {
        return errors.New("user exists")
    }
    password, _ = bservice.New().PasswordHash(password)
    err = dao.NewUser().Reg(username, password)
    return
}

func (*User) ListData(page, limit int64) (list []model.User, count int64, err error) {
    list, count, err = dao.NewUser().ListData(page, limit)
    return
}
